// 1. 大数相加
// 2. 大数相乘
// 3. 小数相加
// 4. 千分位分割 /(?=\B(\d{3})+$)/g
// 5. sum 递归求和
// 红包算法
// 路径问题
// a == 1 && a == 2 && a == 3 等式成立 valueof
// const [a, b] = {a: 1, b:2} 等式可执行 Symbol.iterator 
// 实现add[1][2][3]+ 100 object 里 proxy + Symbol.toPremitive
// 根据子节点上的所有父节点转换为树 (https://blog.csdn.net/qq_43351090/article/details/130359696)

function bigNumAdd(num1, num2) {
  let s1 = num1.toString().length
  let s2 = num2.toString().length
  let max = Math.max(s1, s2)

  let str1 = num1.toString().padStart(max, '0')
  let str2 = num2.toString().padStart(max, '0')
  let res = []
  let dig = 0

  for (let i = max - 1; i >= 0; i--) {
    let sum = parseInt(str1[i]) + parseInt(str2[i]) + dig
    res[i] = sum % 10
    dig = Math.floor(res[i] / 10)
  }
  return res.join('')
}
console.log(bigNumAdd(123, 2))


function floatNumAdd(num1, num2) {
  let s1 = num1.toString().split('.')[1].length
  let s2 = num2.toString().split('.')[1].length
  let max = Math.max(s1, s2)
  let pow = Math.pow(10, max)

  let n1 = num1 * pow
  let n2 = num2 * pow

  return (n1 + n2) / pow
}
console.log(floatNumAdd(0.23, 0.14))


function bigNumMutil(num1, num2) {
  let s1 = num1.toString().length;
  let s2 = num2.toString().length;

  let str1 = num1.toString();
  let str2 = num2.toString();
  let res = [];
  for (let i = s1 - 1; i >= 0; i--) {
    for (let j = s2 - 1; j >= 0; j--) {
      let m = i + j;
      let n = i + j + 1;
      let sum = str1[i] * str2[j] + (res[n] || 0)
      res[n] = sum % 10
      res[m] = Math.floor(sum / 10) + (res[m] || 0)
    }
  }

  return res.join('').replace(/^[0+]/g, '')
}
console.log(bigNumMutil(12, 28))


function spiltNum(num) {
  let res = ''
}


function sum(arr) {
  function fn(i) {
    return i >= arr.length ? 0 : arr[i] + fn(i + 1)
  }
  return fn(0)
}
console.log(sum([1, 2, 3]))


function redPackage(total, count, min) {
  let res = [] // 红包数量
  let remian = total // 剩余金额

  for (let i = 0; i < count; i++) {
    if (i === count - 1) {
      res.push(remian.toFixed(2))
    } else {
      let remianCount = count - i - 1
      let max = remian - (remianCount * min)
      let price = (Math.random() * (max - min) + min).toFixed(2)
      remian = remian - price
      res.push(price)
    }
  }
  return res;
}
console.log(redPackage(5, 5, 0.1))


// 路径解析
function findAllPaths(arr, start, end) {
    const graph = {}
    arr.forEach(item => {
      const [start, end] = item.split('-');
      if(!graph[start]) graph[start] = []
      graph[start].push(end)
    })

    const paths = []
    const visited = new Set()

    function dfs(currentNode, path) {
        if(currentNode === end){
          paths.push(path) // 找到一条路径
          return
        }
        visited.add(currentNode) // 标记当前搜索
        for(let i = 0; i< graph[currentNode]?.length || [].length; i++){
          if(!visited.has(graph[currentNode][i])){
            dfs(graph[currentNode][i], path.concat(graph[currentNode][i])) // 
          }
        }
        visited.delete(currentNode) // 衣橱当前标记
    }
    dfs(start, [start])
    return paths

}// 使用示例
const arr = ['a-b', 'b-c', 'a-d', 'c-d', 'd-f'];
const start = 'a';
const end = 'f';
const allPaths = findAllPaths(arr, start, end);
console.log(allPaths);


function findLongestIdiomChain(arr) {
  const graph = {}
  arr.forEach(item => {
    const start = item[0]
    if (!graph[start]) graph[start] = []
    graph[start].push(item)
  })

  let res = 0
  for (let i = 0; i < arr.length; i++) {
    const visited = new Set()
    let paths = dfs([], arr[i], visited)

    res = Math.max(paths.length, res)
  }

  function dfs(temp, currentNode, visited) {
    visited.add(currentNode) // 标记当前搜索
    let key = currentNode[currentNode.length - 1]
    temp.push(currentNode)
    for (let i = 0; i < graph[key]?.length || [].length; i++) {
      if (!visited.has(graph[key][i])) {
        let paths = dfs(temp.slice(), graph[key][i], visited) // 
        if(paths.length > temp.length){
          temp = paths
        }
      }
    }
    visited.delete(currentNode) // 衣橱当前标记
    return temp
  }

  return res
}
// 示例用法
const idioms = ["接踵而来", "来日方长", "长生久视", "视死如归", "归心似箭", "1箭在弦上"];
console.log(findLongestIdiomChain(idioms), '成语接龙'); // 输出最长长度



// 等式成立
var a = {
  n: 1,
  valueOf: function () {
    return this.n++
  }
}
console.log(a == 1 && a == 2 && a == 3)


// add[1][2][3] proxy 　[]是get 的用法
const add = new Proxy({ value: 0 }, {
  get(target, key) {
    if (key === Symbol.toPrimitive) {
      return () => target.value
    }
    let newVal = target.value + Number(key)
    return new Proxy({ value: newVal }, this)
  }
})
console.log(add[1][2][3])



const filterTree = (arr, id = null) => {
  // 第一次id未赋值，返回根节点
  // 查找parentId为此id的项，并进行递归调用
  return arr.filter(e => e.parentId === id)
    .map(e => {
      e.children = filterTree(arr, e.id)
      return e
    })
}
// 使用
const test = [
  {
      "id": 1,
      "name": "bug菜单",
      "parentId": null,
      "type": 1
  },
  {
      "id": 2,
      "name": "404",
      "parentId": null,
      "type": 1
  },
  {
      "id": 3,
      "name": "123",
      "parentId": 1,
      "type": 1
  },
  {
      "id": 4,
      "name": "222",
      "parentId": 1,
      "type": 1
  },
  {
      "id": 7,
      "name": "333",
      "parentId": 3,
      "type": 1
  },
  {
      "id": 8,
      "name": "777",
      "parentId": 7,
      "type": 1
  },
  {
      "id": 10,
      "name": "888",
      "parentId": 8,
      "type": 1
  },
  {
      "id": 11,
      "name": "999",
      "parentId": null,
      "type": 1
  },
  {
      "id": 12,
      "name": "10",
      "parentId": 10,
      "type": 1
  }
]
console.log(filterTree(test))



function findSelectedIds(node) {
  let result = [];
  
  if (node.selected) {
    result.push(node.id);
  }
  
  if (node.children && node.children.length > 0) {
    for (let i = 0; i < node.children.length; i++) {
      const childIds = findSelectedIds(node.children[i]);
      result = result.concat(childIds);
    }
  }
  
  return result;
}
